cap log close
snapshot erase _all
clear all
estimates drop _all

*===========================*
*	Set Globals				*
*===========================*

	global filenum 20
	global filename "main_tables"

*===========================*
*	Set Locals				*
*===========================*
	
	#delimit ;
		local m_list 		"12 1 3 4 5 6";
		
		local keep_list1 	"m12_x_small_y2020 m1_x_small_y2020 
							 m3_x_small_y2020 m4to6_x_small_y2020";
							 
		local keep_list2 	"m12_x_small_y2020 m1_x_small_y2020 
							 m3_x_small_y2020 m4_x_small_y2020 m5_x_small_y2020 
							 m6_x_small_y2020";							 

		local keep_list3 	"m12_x_small_y2020 m1_x_small_y2020 
							 m3_x_small_y2020 m4_x_small_y2020 m5_x_small_y2020 
							 m6_x_small_y2020 m4to6_x_small_y2020";	
						 
		local fixed_effect	"datevar#statefip"; // can add ind_2digit where appropriate;
		
		local outcomes 		"absent_paid inlf";
	
	#delimit cr
	
*===========================*
*	Load In Analysis File	*
*===========================*
	log using "${log_path}/${filenum}_${filename}.smcl", replace
	
	use "${data_path}/analysis_file.dta" , clear
	
	label var m12_x_small_y2020 	"December"
	label var m1_x_small_y2020 		"January"
	label var m3_x_small_y2020 		"March"
	label var m4_x_small_y2020 		"April"
	label var m5_x_small_y2020 		"May"
	label var m6_x_small_y2020 		"June"

	replace gender_parent = . 	if cdc_levels_m4 != 4		// only want to cut for high april
	
	snapshot save
	
*===================*
* 	DD in Time		*
* 	18/19 vs 20		*
*===================*


foreach v in `outcomes'{
	
	**** Columns 1 and 2: Separate by Telework	
		forvalues t=0/1{ // telework loop		
			snapshot restore 1
			
			keep if tw_binary_10ly==`t'
			
			#delimit ;
								
				** DYNAMIC DD **;
					reghdfe `v' 
							m12-m6 y2020 small
							small_x_y2020
							m12_x_y2020-m6_x_y2020
							m12_x_small-m6_x_small
							m12_x_small_y2020 - m6_x_small_y2020
							[w=wtfinl], a(`fixed_effect') vce(cluster cpsidp);
										
					test m12_x_small_y2020 = m1_x_small_y2020 = m3_x_small_y2020 = 0;
						scalar pval=`r(p)';					

					qui sum `v' if asec_year!=2020 & small==1 [w=wtfinl];
						scalar control_mean = `r(mean)';

					est sto ddd1_tw`t';
					estadd local 	controls 	"NO";
					estadd local 	spec		"DDD";
					estadd local 	samp 		"TW `t'";
					estadd scalar 	cmean 		control_mean;
					estadd scalar 	parallel 	pval;	
												
				** DYNAMIC DD WITH CONTROLS **;
				
					reghdfe `v' 
							m12-m6 y2020 small
							small_x_y2020
							m12_x_y2020-m6_x_y2020
							m12_x_small-m6_x_small
							m12_x_small_y2020 - m6_x_small_y2020
							bachelors age age2 childUnder12
							[w=wtfinl], a(`fixed_effect') vce(cluster cpsidp);
								
					test m12_x_small_y2020 = m1_x_small_y2020 = m3_x_small_y2020 = 0;
						scalar pval=`r(p)';					
									
					est sto ddd2_tw`t';
					estadd local  	controls 	"YES";
					estadd local 	spec		"DDD";
					estadd local 	samp 		"TW `t'";
					estadd scalar 	cmean 		control_mean;
					estadd scalar 	parallel 	pval;	
				
			#delimit cr
			
		} // end telework loop
	
	*** Columns 3 - 6 : By CDC Levels in April 
		snapshot restore 1

		local c = 0
		
		foreach sample_var in cdc_levels_m4 gender_parent{
			
			local c = `c' +1
			
			levelsof `sample_var', local(samp_levels)		

			foreach x in `samp_levels'{
				
				snapshot restore 1
				
				keep if `sample_var'==`x' &  tw_binary_10ly==0
				
				#delimit ;
				
					** NO CONTROLS **;
					reghdfe `v' 
							m12-m6 y2020 small
							small_x_y2020
							m12_x_y2020-m6_x_y2020
							m12_x_small-m6_x_small
							m12_x_small_y2020 - m6_x_small_y2020
							[w=wtfinl], a(`fixed_effect') vce(cluster cpsidp);
										
					test m12_x_small_y2020 = m1_x_small_y2020 = m3_x_small_y2020 = 0;
						scalar pval=`r(p)';					

					qui sum `v' if asec_year!=2020 & small==1 [w=wtfinl];
						scalar control_mean = `r(mean)';

					est sto ddd_c`c'_x`x';
					estadd local 	controls 	"NO";
					estadd local 	spec		"DDD";
					estadd local 	samp 		"`samp' `x'";
					estadd scalar 	cmean 		control_mean;
					estadd scalar 	parallel 	pval;	
								
					** WITH CONTROLS **;
					
					reghdfe `v' 
							m12-m6 y2020 small
							small_x_y2020
							m12_x_y2020-m6_x_y2020
							m12_x_small-m6_x_small
							m12_x_small_y2020 - m6_x_small_y2020
							bachelors age age2 childUnder12
							[w=wtfinl], a(`fixed_effect') vce(cluster cpsidp);
								
					test m12_x_small_y2020 = m1_x_small_y2020 = m3_x_small_y2020 = 0;
						scalar pval=`r(p)';					
									
					est sto ddd2_c`c'_x`x';
					estadd local  	controls 	"YES";
					estadd local 	spec		"DDD";
					estadd local 	samp 		"`samp' `x'";
					estadd scalar 	cmean 		control_mean;
					estadd scalar 	parallel 	pval;	
									
				#delimit cr
				
			} // end subsample levels loop	
			
		}	// end subsample loop
		
	** MAKE TABLES **
	
		*** Main Table 	***
		#delimit ;
			esttab 	ddd2_tw0 ddd2_tw1 ddd2_c1_x*
				using "${out_path}/${filenum}_`v'_main.tex",
				replace	label se star(* 0.1 ** .05 *** 0.01)
				s(cmean spec samp controls parallel N, 
					label("Control Mean" "Model" "Sample" "Controls" "Parallel Trends" "Observations"))
				keep(`keep_list2');	
		
		#delimit cr
		
		*** Gender + Parents Table 	***
		#delimit ;
			esttab 	ddd2_c2_x* 
				using "${out_path}/${filenum}_`v'_gender_parent.tex",
				replace	label se star(* 0.1 ** .05 *** 0.01)
				s(cmean spec samp controls parallel N, 
					label("Control Mean" "Model" "Sample" "Controls" "Parallel Trends" "Observations"))
				keep(`keep_list2');	
		
		#delimit cr
	
} // end outcomes loop
log close
